package com.example.moviegroup.adapter;

import android.content.Context;
import android.os.Environment;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.moviegroup.R;
import com.example.moviegroup.app.Logger;
import com.example.moviegroup.bean.FilmBean;
import com.example.moviegroup.utils.MyUrls;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapCacheListener;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig;
import com.lidroid.xutils.bitmap.core.BitmapSize;
import com.lidroid.xutils.cache.FileNameGenerator;

import java.io.File;
import java.util.ArrayList;

/**
 * Created by Xiaodong on 2015/11/18 0018.
 */
public class MyPagerAdapter extends PagerAdapter{
    private  ArrayList<FilmBean.Film> list =new ArrayList<FilmBean.Film>();
    private  Context context;
    private BitmapUtils bitmapUtils;
    private File file;
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }
    public MyPagerAdapter(Context context){
        this.context=context;
        file = new File(Environment.getExternalStorageDirectory()+File.separator+"MovieGroup");
        bitmapUtils=new BitmapUtils(context,file.getAbsolutePath(),100*1024*1024);
    }
    public void setList(ArrayList<FilmBean.Film> list){
        this.list=list;
    }
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        // TODO Auto-generated method stub
        return arg0==arg1;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Logger.d(position+"position");
       RelativeLayout v= (RelativeLayout) View.inflate(context, R.layout.vp_item, null);
        ImageView iv= (ImageView) v.findViewById(R.id.iv_pager);
        BitmapDisplayConfig displayConfig = new BitmapDisplayConfig();
        //设置图片的大小
       // displayConfig.setBitmapMaxSize(new BitmapSize(400, 400));
        //设置图片下载过程中iv上显示的临时图片
        displayConfig.setLoadingDrawable(context.getResources().getDrawable(R.mipmap.ic_launcher));
        //设置图片下载失败iv上显示的图片
        displayConfig.setLoadFailedDrawable(context.getResources().getDrawable(R.mipmap.ic_launcher));
        AlphaAnimation animation=new AlphaAnimation(0.2f,1.0f);
        animation.setDuration(500);
        displayConfig.setAnimation(animation);
        Logger.d(MyUrls.MV_BASE + list.get(position).photo_url);
        bitmapUtils.display(iv, MyUrls.MV_BASE + list.get(position).photo_url, displayConfig);
        container.addView(v);

        return v;
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View)object);
    }

}
